package com.example.aidrawavatar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import android.os.Bundle;
import android.view.View;
import com.example.aidrawavatar.adapter.MainActivityAdapter;
import com.example.aidrawavatar.databinding.ActivityMainBinding;
import com.example.aidrawavatar.utils.RetrofitUtils;
import java.io.IOException;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initView();
        initListener();
    }

    private void initListener(){
        binding.activityMainButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.activityMainButton.setVisibility(View.INVISIBLE);
                RetrofitUtils.getInstance().getImageService().getImage().enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        try {
                            if(response.body().string().equals("finish")){
                                binding.activityMainText.setText(response.body().string());
                                binding.activityMainRecycleView.getAdapter().notifyDataSetChanged();
                                binding.activityMainButton.setVisibility(View.VISIBLE);
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {

                    }
                });
            }
        });
    }

    private void initView(){
        binding.activityMainRecycleView.setLayoutManager(new GridLayoutManager(getApplicationContext(),3));
        binding.activityMainRecycleView.setAdapter(new MainActivityAdapter());
    }

    @Override
    protected void onDestroy() {
        binding = null;
        super.onDestroy();
    }
}